<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 偷菜接口
 */
class Toucai extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $this->user_id = $this->auth->id;
        $this->pagesize = input('pagesize')?input('pagesize'):6;
        $this->page = input('page')?input('page'):1;
        // $this->user_id = 2;
    } 
    /**
     * 好友列表tou_status=1代表有东西偷
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/toucai/list)
     * @ApiParams   (name="page",type=int,required=true,description="当前页码数")
     * @ApiParams   (name="pagesize",type=int,required=true,description="每页显示多少个,不传默认六个")
     * @ApiReturnParams   (name="total", type="integer", required=true, sample="10",description="页码总数")
     * @ApiReturn({

     })
     */
    public function list(){
      $return = Db::name('user as u')->field("u.id,u.avatar,u.nickname,IF(EXISTS(SELECT * FROM fa_new_my_plant WHERE not_pick_num!=0.00 AND user_id=u.id),1,0) AS tou_status")->where('u.id','neq',$this->user_id)->page($this->page,$this->pagesize)->select();
      $this->success('好友列表',$return);
    } 
 
    /**
     * 好友详情
     * @ApiSummary  ()
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/toucai/friend_detail)
     * @ApiParams   (name="id",type=int,required=true,description="用户id")
     * @ApiReturn({

     })
     */
    public function friend_detail(){
      $user_id = input('id');
      $return['userinfo'] = Db::name('user')->field('avatar,nickname,id')->where('id',$user_id)->find();
      $return['list'] = Db::name('new_my_plant')->where('user_id',$user_id)->field('image,id as plant_id,id,shop_id,per,not_pick_num,IF(not_pick_num!=0.00,1,0) AS tou_status')->select();
      $this->success('好友详情',$return);
    } 

     /**
     * 偷菜操作
     * @ApiSummary  ()
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/toucai/tou)
     * @ApiParams   (name="plant_id",type=int,required=true,description="plant_id从用户植物详情接口获取")
     * @ApiReturn({
      
     })
     */   
    public function tou(){
      $new_my_plant = Db::name('new_my_plant')->where('id',input('plant_id'))->find();
      if($new_my_plant['not_pick_num']==0.00)$this->error('当前没东西可偷摘');

      $if_exist_cangku = Db::name('cangku')->where(['user_id'=>$this->user_id,'status'=>'guoshi','shop_id'=>$new_my_plant['shop_id']])->find();
         
      if($if_exist_cangku){//如果该果实在仓库了
        $new_cangku_data['updatetime'] = time();
        $new_cangku_data['weigh'] = $if_exist_cangku['weigh']+$new_my_plant['not_pick_num'];
        $res = Db::name('cangku')->where(['user_id'=>$this->user_id,'status'=>'guoshi','shop_id'=>$new_my_plant['shop_id']])->update($new_cangku_data);//修改仓库中该果实的收成
        
      }else{
        $new_cangku_data = Db::name('shop')->where('id',$new_my_plant['shop_id'])->field('id as shop_id,image,text,name')->find(); 

        $new_cangku_data['weigh'] = $new_my_plant['not_pick_num'];
        $new_cangku_data['updatetime'] = time();
        $new_cangku_data['status'] = 'guoshi';
        $new_cangku_data['user_id'] = $this->user_id;
       
        $res = Db::name('cangku')->insert($new_cangku_data);//插入一条果实到仓库
      }

      if($res){
        $touzhai_log['user_id'] = $this->user_id;
        $touzhai_log['from_user_id'] = $new_my_plant['user_id'];
        $touzhai_log['text'] = $this->auth->nickname.'偷了'.$new_my_plant['name'].$new_my_plant['not_pick_num'].'斤';
        $touzhai_log['createtime'] = time();
        Db::name('touzhai_log')->insert($touzhai_log);

        Db::name('new_my_plant')->where('id',input('plant_id'))->update(['not_pick_num'=>0]);
        $this->success('成功偷摘');
      }

    }

  

}
